<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Set options to session
     */
    protected function _initConfig()
    {
        Zend_Registry::set('config', new Zend_Config($this->getOptions(), true));

    }

    protected function _initLogger()
    {
        $logger = \G4T\Log\LogFactory::getLogger();
        \Zend_Registry::set('g4t.log', $logger);
    }


    public function _initAutoloaderNamespaces()
    {
        require_once APPLICATION_PATH . '/../library/Doctrine/Common/ClassLoader.php';

        $autoloader = \Zend_Loader_Autoloader::getInstance();
        $fmmAutoloader = new \Doctrine\Common\ClassLoader('Bisna');
        $autoloader->pushAutoloader(array($fmmAutoloader, 'loadClass'), 'Bisna');
    }

    /**
     * Initialize the Cache Manager
     * Initializes the memcached cache into
     * the registry and returns the cache manager.
     *
     * @return Zend_Cache_Manager
     */
    protected function _initCachemanager()
    {
        $cachemanager = $this->getPluginResource('cachemanager')
            ->init();

        // v sluchae productiona i bolshih projectov add version - chtob ne dropat cache
        $appVersion = 1;

        $opt = $this->getOptions();
        $prefix = $opt['resources']['cachemanager']['memcached']['projectname'];

        $memcached = $cachemanager->getCache('memcached');
        $memcached->setOption('cache_id_prefix', $prefix.APPLICATION_ENV .'_' . $appVersion);

        return $cachemanager;
    }

    /**
     * Initialize the Session Id
     * This code initializes the session and then
     * will ensure that we force them into an id to
     * prevent session fixation / hijacking.
     *
     * @return void
     */
    protected function _initSessionId()
    {
        $this->bootstrap('session');
        $opts = $this->getOptions();
        if ('Zend_Session_SaveHandler_Cache' == $opts['resources']['session']['saveHandler']['class']) {
            $cache = $this->bootstrap('cachemanager')
                ->getResource('cachemanager')
                ->getCache('memcached');
            $cache = clone $cache;

            $opt = $this->getOptions();
            $prefix = $opt['resources']['cachemanager']['memcached']['projectname'];
            $cache->setOption('cache_id_prefix', $prefix.'_'.APPLICATION_ENV);

            \Zend_Session::getSaveHandler()->setCache($cache);
        }
        $defaultNamespace = new Zend_Session_Namespace();
        if (!isset($defaultNamespace->initialized)) {
            Zend_Session::regenerateId();
            $defaultNamespace->initialized = true;
        }
        \Zend_Session::start();
    }
}

